<template>
    <div v-loading="loading" class="app-container">
        <x-table v-model="searchData" :config="tableConfig" :data="tableData" :page="page" :load="getCinemaByPage" />
    </div>
</template>
    
<script>
import { getCinemaByPage, delCinemaById } from '@/api/cinema'
import { importDic } from '@/utils'
import { getToken } from '@/utils/auth'

export default {
    data() {
        return {
            loading: 0,
            tableData: [],
            page: {
                pageSizes: [10, 20],
                pageSize: 10,
                pageNum: 1,
                total: 0
            },
            loginUserRole: JSON.parse(sessionStorage.getItem("loginUserRole"))[0],
            loginUser: JSON.parse(sessionStorage.getItem("loginUser")),
            searchData: {},
            propId: '',
        }
    },

    computed: {
        tableConfig() {
            const _this = this
            return {
                search: true,
                reset: true,
                stripe: false,
                border: true,
                highlightCurrentRow: true,
                rowClick: node => _this.$emit('cellClick', node),
                btn: [
                    { text: '新增', click: () => _this.operate2('add'), icon: 'el-icon-circle-plus' }
                ],
                column: [
                    { label: '影院名', name: 'cinemaName', search: true, xType: 'input' },
                    { label: '影院地址', name: 'address', search: true, xType: 'input' },
                    { label: '影院状态', name: 'status', search: false, xType: 'select', dic:importDic('cinemaStatus') },
                    { label: '影院负责人', name: 'principal', search: true, xType: 'input' },
                ],
                operate: [
                    { text: '编辑', show: true, click: data => this.operate2('edit', data) },
                    { text: '查看', show: true, click: data => this.operate2('detail', data) },
                    { text: '删除', show: true, click: _this.del },
                ]
            }
        }
    },

    mounted() {
        this.getCinemaByPage()
    },

    methods: {
        getCinemaByPage() {
            this.loading++
            getCinemaByPage(this.searchData, this.loginUser, this.loginUserRole,this.page.pageNum, this.page.pageSize, getToken()).then(res => {
                console.log("getCinemaByPage" + res)
                this.tableData = res.result.records
                this.page.total = res.result.total
            }).catch(e => console.error(e)).finally(() => this.loading--)
        },
        operate2(mode, data) {
            if (mode !== 'add') {
                this.propId = data.id
            }
            this.mode = mode
            this.$router.push({
                path: "/cinema/form",
                query: {
                    mode: this.mode,
                    id: this.propId
                }
            })
        },
        del(data) {
            this.delConfirm().then(() => {
                this.loading++
                delCinemaById(data.id, getToken()).then(res => {
                    this.$message.success(res.message)
                    this.getCinemaByPage()
                }).catch(e => console.log(e)).finally(() => this.loading--)
            }).catch(e => console.log(e))
        }
    },

}
</script>